/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.flushpongle.digr2id.grails.plugins.codecs;

/**
 * <p>Produces SHA-256 hash and provides corresponding hash matcher.</p>
 *
 * <p>This is a typical <a href="http://grails.org/">Grails</a> codec,
 * which adds <tt>encodeAsSha256</tt> method to all {@link String} instances
 * within <a href="http://grails.org/">Grails</a> application at runtime.</p>
 *
 * <p>Encoding password on user registration in controller:</p>
 *
 * <p><pre>
 *     String passwordHash = params.password.encodeAsSha256()
 * </pre></p>
 *
 * <p>Checking for password match:</p>
 *
 * <p><pre>
 *     if (Sha256Codec.match(params.password, passwordHashFromDatabase)) {
 *         ...
 *     }
 * </pre></p>
 *
 * @author Vladimir Tsukur
 */
class Sha256Codec {

    static encode = { str ->
        new DigesterAndBase64Encoder().process(str, "SHA-256")
    }

    static boolean match(String input, String hash) {
        hash == encode(input)
    }

}
